<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增视察')"/>
    <th:block th:include="include :: summernote-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-inspect-add">
        <div class="form-group">
            <label class="col-sm-3 control-label">视察标题：</label>
            <div class="col-sm-8">
                <input name="inspectTitle" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">封面图：</label>
            <div class="col-sm-8 layui-upload">
                <div class="layui-upload-list">
                    <input id="inspectPic" name="inspectPic" class="form-control" type="hidden">
                    <img  class="layui-upload-img" id="pic"  style="width: 220px;height: 180px;" >
                </div>
                <button type="button" class="layui-btn" id="addBtn">上传图片</button>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">视察内容：</label>
            <div class="col-sm-8">
                <input id="inspectContent" name="inspectContent" class="form-control" type="hidden">
                <div class="summernote"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <input name="remark" class="form-control" type="text">
            </div>

        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: summernote-js"/>
<script type="text/javascript">
    var prefix = ctx + "module/inspect"

    $('.summernote').summernote({
        placeholder: '请输入视察内容',
        height: 400,
        lang: 'zh-CN',
        followingToolbar: false,
        callbacks: {
            onImageUpload: function (files) {
                sendFile(files[0], this);
            }
        }
    });

    // 上传文件
    function sendFile(file, obj) {
        var data = new FormData();
        data.append("file", file);
        $.ajax({
            type: "POST",
            url: ctx + "common/upload",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $(obj).summernote('editor.insertImage', result.url, result.fileName);
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function (error) {
                $.modal.alertWarning("图片上传失败。");
            }
        });
    }


    $("#form-inspect-add").validate({
        focusCleanup: true
    });


    //上传图片
    layui.use(['upload'], function () {
        var $ = layui.jquery
            , upload = layui.upload;
        //普通图片上传
        upload.render({
            elem: '#addBtn'
            , url: ctx + 'common/uploadImage_QiNiu'
            , size: 10000
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $("#pic").attr("src", result);
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功
                $("#inspectPic").val(res.data.src);
            }
        });
    });



    function submitHandler() {
        if ($.validate.form()) {
            var pic =$("#inspectPic").val();
            if(pic == null || pic == undefined || pic == '' ){
                $.modal.alertWarning("请上传图片！");
                return;
            }
            var sHTML = $('.summernote').summernote('code');
            $("#inspectContent").val(sHTML);
            $.operate.save(prefix + "/add", $('#form-inspect-add').serialize());
        }
    }
</script>
</body>
</html>